<template>
    <div class="box">
        <ArrowLeft @click="close" class="close-icon"/>
        <div class="header-send">发红包</div>
        <Close @click="close" class="close-icon"/>
        <el-form class='red-envelope-form' :model="redEnvelope" label-width="150px">
            <el-form-item label="祝福">
                <el-input class="red-envelope-input" placeholder="恭喜发财，身体健康" v-model="redEnvelope.message"/>
            </el-form-item>
            <el-form-item label="红包金额">
                <el-input class="red-envelope-input" v-model="redEnvelope.totalAmount" :rules="[{type: Number, message: '必须为数字'}, {required: true, message: '不能为空'}]"/>
            </el-form-item>
            <el-form-item label="红包数量">
                <el-input class="red-envelope-input" v-model="redEnvelope.totalCount" :rules="[{type: Number, message: '必须为数字'}, {required: true, message: '不能为空'}]"/>
            </el-form-item>
        </el-form>
        <div class='red-envelope-form'>
            <el-button class="red-envelope-button" type="danger" @click="SendRedEnvelope">塞钱进红包</el-button>
        </div>
    </div>

</template>
  
<script lang="ts" setup>
import { reactive } from 'vue'
import { post } from '@/utils/request'
import { ElMessage } from 'element-plus';
const sendApi = process.env.VUE_APP_SEND_API
const props = defineProps({userName: String, topicId: String})
const redEnvelope = reactive(
{
    totalCount: 0,
    message: "恭喜发财，身体健康",
    totalAmount: 0,
    userName: props.userName,
    topicId: props.topicId
}
)
const emits = defineEmits(["changeShow"])
const close = () => {emits("changeShow", false)}
const SendRedEnvelope = () => {
post(sendApi, redEnvelope).then(
    (res) => {
        console.log(res)
        if (res.data){
            ElMessage({message: '发送成功', type: 'success'})
            emits("changeShow", false)
        }else{
            ElMessage({message: '发送失败，请稍后再试', type: 'error'})
        }
    }
).catch(() => {ElMessage({message: '发送失败，请稍后再试', type: 'error'})})
}
</script>
<style scoped>
.red-envelope-input {
    width: 70%;
}
.red-envelope-form {
    width: 100%;
    height: 25%;
}
.red-envelope-button {
    margin-left: 39%;
}
.box {
    flex-wrap: wrap;
    display: flex;
    height: auto;
    padding: 5px;
}
.close-icon {
    padding: 5px;
    width: 10%; 
    height: 8%; 
}
.header-send {
    display: flex;
    height: 8%;
    width: 80%;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    text-shadow: 0 0 black;
    text-transform: capitalize;
  }
</style>